package com.bdqn.easybuy1.servlet.pre;

import com.bdqn.easybuy1.entities.News;
import com.bdqn.easybuy1.entities.Product;
import com.bdqn.easybuy1.param.NewsParams;
import com.bdqn.easybuy1.service.news.NewsService;
import com.bdqn.easybuy1.service.news.NewsServiceImpl;
import com.bdqn.easybuy1.service.product.ProductCategoryService;
import com.bdqn.easybuy1.service.product.ProductCategoryServiceImpl;
import com.bdqn.easybuy1.service.product.ProductService;
import com.bdqn.easybuy1.service.product.productServiceImpl;
import com.bdqn.easybuy1.servlet.AbstractServlet;
import com.bdqn.easybuy1.utils.Pager;
import com.bdqn.easybuy1.utils.ProductCategoryVo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.util.List;

@WebServlet(name = "HomeServlet", value = "/Home")
public class HomeServlet extends AbstractServlet {

    private ProductCategoryService productCategoryService;
    private ProductService productService;
    private NewsService newsService;

    @Override
    public void init() throws ServletException {
        productCategoryService = new ProductCategoryServiceImpl();
        newsService = new NewsServiceImpl();
        productService = new productServiceImpl();
    }

    public String index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // ①、查询首页轮播图右侧的公告信息
        Pager pager = new Pager(5, 5, 1);
        NewsParams params = new NewsParams();
        params.openPage((pager.getCurrentPage() - 1) * pager.getRowPerPage(), pager.getRowPerPage());
        List<News> news = newsService.queryNewsList(params);

        // ②、查询商品分类
        List<ProductCategoryVo> productCategoryVoList = productCategoryService.queryAllProductCategoryList();
        //查询一楼
        for (ProductCategoryVo vo : productCategoryVoList) {
            List<Product> productList = productService.getProductList(1, 8, null, vo.getProductCategory().getId(), 1);
            vo.setProductList(productList);
        }

        // ③、封装数据进入request域，因为跳转使用的是转发
        request.setAttribute("productCategoryVoList", productCategoryVoList);
        request.setAttribute("news", news);

        // ④、跳转到指定的JSP页面
        return "/pre/index";
    }
}
